package stephen.com.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import stephen.com.dao.base.BaseDao;

import stephen.com.model.UserInfo;
import stephen.com.utils.JDBCUtil;
import java.sql.SQLException;
import java.util.Map;

public class UserInfoDao extends BaseDao<UserInfo> {

	/**
	 * 获取用户详细信息的sql
	 *
	 * @param userId 用户ID
	 * @return Map对象
	 */
	public Map<String, Object> getDetail(Integer userId) {
		QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
		Map<String, Object> map = null;
		try {
			String sql = "select ui.user_id as userId,ui.real_name as realName,ui.age,ui.gender,u.is_secret " +
					"as isSecret," + "ui.phone,ui.describe as introduce,ui.register_time as registerTime,ui" +
					".login_time as loginTime," + "ui.pic,ui.look,u.dept_id as deptId,d.`name` as deptName," + "(select count(*) as total from user_focus where user_focus_id = ui.user_id) as follows " + "from user_info ui " + "left join `user` u on u.id = ui.user_id " + "left join dept d on d.id = u.dept_id " + "where ui.user_id = ?";

			map = qr.query(sql, new MapHandler(), userId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;
	}
}
